package com.fufufu.common.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Lenovo
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set = new HashSet<>();

    @Override
    public void initialize(ListValue constraintAnnotation) {
        // 初始化，将允许的值保存至对象属性中以让别的方法使用
        for (int val : constraintAnnotation.vals()) {
            set.add(val);
        }

    }

    /**
     *
     * @param integer 提交的值，也就是需要校验的值
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {

        return set.contains(integer);
    }
}
